package com.ftg.learn.mapper.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Objects;

/**
 * SQL模拟器
 * @author wangty
 * @date 2022/10/19
 */
public class SaleMapperProvider {

    public String likeWithNameAddressSql(@Param("name") String name, @Param("address") String address) {
        SQL sql = new SQL().SELECT("*")
                .FROM("sale")
                .WHERE("flag = 0");

        if (Objects.nonNull(name)) {
            sql.AND().WHERE("name like concat(#{name},'%') || name like concat('%',#{name}) || name like concat('%',#{name},'%')");
        }
        if (Objects.nonNull(address)) {
            sql.AND().WHERE("address like concat(#{address},'%') || address = (#{address} )|| address like concat('%',#{address},'%') ");
        }
        return sql.toString();
    }


    public String pageWithNameAddressSql(@Param("start") int start, @Param("limit") Integer limit, @Param("name") String name, @Param("address")String address){
        SQL sql = new SQL().SELECT("*")
                .FROM("sale")
                .WHERE("flag = 0");

        if (Objects.nonNull(name)) {
            sql.AND().WHERE("name like concat(#{name},'%')");
        }

        if (Objects.nonNull(address)) {
            sql.AND().WHERE("address like concat(#{address},'%')");
        }
        sql.OFFSET("#{start}").LIMIT("#{limit}");
        return sql.toString();
    }

    public String pageWithNameAddressCountSql( @Param("name") String name, @Param("address")String address){
        SQL sql = new SQL().SELECT("count(id) num")
                .FROM("sale")
                .WHERE("flag = 0");

        if (Objects.nonNull(name)) {
            sql.AND().WHERE("name like concat(#{name},'%')");
        }

        if (Objects.nonNull(address)) {
            sql.AND().WHERE("address like concat(#{address},'%')");
        }
        return sql.toString();
    }
}
